﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Diagnostics;

namespace TrenchcoatRobots.Classes.ComponentClasses {
	/// <summary>
	/// TextureRenderer renders a texture to the screen according to the angle & position of the base object.
	/// </summary>
	class TextureRenderer : Component {
		public Texture2D Texture { get; set; }
		public bool FlipHorizontal { get; set; }
		public bool FlipVertical { get; set; }

		#region Constructors
		/// <summary>
		/// Creates a new TexturedObject at 0,0 with the rotation 0.
		/// </summary>
		/// <param name="gameObject">The GameObject to which the Rigidbody will belong.</param>
		/// <param name="texture">Texture for the TexturedObject</param>
		public TextureRenderer( ParentMethod gameObjectMethod, Texture2D texture )
			: base( gameObjectMethod, "TextureRenderer", false ) {
			this.Texture = texture;
			FlipHorizontal = false;
			FlipVertical = false;
		}
		#endregion

		public override void Draw( ref SpriteBatch spriteBatch, ref TrenchcoatRobots.Datatypes.DrawInfo drawinfo ) {
			if (FlipHorizontal && FlipVertical) {
				spriteBatch.Draw(
					Texture,
					(Parent().Position + drawinfo.Camera.Position) * drawinfo.Camera.Zoom,
					new Rectangle(0,0,Texture.Width,Texture.Height),
					Color.White,
					MathHelper.ToRadians(Parent().Angle + 180),
					new Vector2(Texture.Width, Texture.Height) * 0.5f,
					1.0f * drawinfo.Camera.Zoom,
					SpriteEffects.None,
					0f
					);
			}
			else if (FlipHorizontal) {
				spriteBatch.Draw(
					Texture,
					(Parent().Position + drawinfo.Camera.Position) * drawinfo.Camera.Zoom,
					new Rectangle(0,0,Texture.Width,Texture.Height),
					Color.White,
					MathHelper.ToRadians(Parent().Angle),
					new Vector2(Texture.Width, Texture.Height) * 0.5f,
					1.0f * drawinfo.Camera.Zoom,
					SpriteEffects.FlipHorizontally,
					0f
					);
			}
			else if (FlipVertical) {
				spriteBatch.Draw(
					Texture,
					(Parent().Position + drawinfo.Camera.Position) * drawinfo.Camera.Zoom,
					new Rectangle(0,0,Texture.Width,Texture.Height),
					Color.White,
					MathHelper.ToRadians(Parent().Angle),
					new Vector2(Texture.Width, Texture.Height) * 0.5f,
					1.0f * drawinfo.Camera.Zoom,
					SpriteEffects.FlipVertically,
					0f
					);
			}
			else {
				spriteBatch.Draw(
					Texture,
					(Parent().Position + drawinfo.Camera.Position) * drawinfo.Camera.Zoom,
					new Rectangle(0,0,Texture.Width,Texture.Height),
					Color.White,
					MathHelper.ToRadians(Parent().Angle),
					new Vector2(Texture.Width, Texture.Height) * 0.5f,
					1.0f * drawinfo.Camera.Zoom,
					SpriteEffects.None,
					0f
					);
			}
		}
	}
}
